import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;

public class Listener extends Thread {
	private MulticastSocket socket;
	private String name;

	public Listener(MulticastSocket socket, String name) {
		this.socket = socket;
		this.name = name;
	}

	public void run() {
		while (!socket.isClosed()) {
			byte[] datagram = new byte[256];
			DatagramPacket packet = new DatagramPacket(datagram, 256);
			try {
				socket.receive(packet);
			} catch (IOException e) {
				if (socket.isClosed()) {
					break;
				}
				e.printStackTrace();
			}
			datagram = packet.getData();
			Message.checkAndPrintMessage(datagram, name);
		}
	}
}
